package com.crp.unitalinforbaseplat.app.unitalinforbaseplat.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.crp.unitalinforbaseplat.app.core.BaseController;
import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.core.MessageUtil;
import com.crp.unitalinforbaseplat.app.core.utils.EmptyUtils;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.PageBean;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.Store;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 仓库信息表 前端控制器
 * </p>
 *
 * @author zsx
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {

    @Autowired
    IStoreService storeService;
    @RequestMapping("/list_index")
    ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/store/list");
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }
    @RequestMapping("/add_index")
    ModelAndView addIndex(Store store) {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/store/add");
        modelAndView.addObject("roleName",getRoleName());
        if(!EmptyUtils.isEmpty(store.getId())){
            modelAndView.addObject("store",storeService.getById(store.getId()));
        }else{
            modelAndView.addObject("store",store);
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/list")
    PageBean<Store> index(Store store) {
        PageBean<Store> pageBean = new PageBean<Store>();
        List<Store> storeList = storeService.list(new LambdaQueryWrapper<Store>().like(StringUtils.isNotBlank(store.getName()),Store::getName,store.getName()));
        pageBean.setData(storeList);
        return pageBean;
    }

    @ResponseBody
    @RequestMapping("/delete")
    Message delete(Store store) {
        storeService.removeById(store.getId());
        return MessageUtil.createMessage(true,"删除成功");
    }

    @ResponseBody
    @RequestMapping("/add")
    Message add(Store store) {
        store.setCreateUserId(getId().intValue());
        store.setCreateTime(new Date());
        if(EmptyUtils.isEmpty(store.getId())){
            if(storeService.save(store)){
                return MessageUtil.createMessage(true,"新增成功");
            }else {
                return MessageUtil.createMessage(false,"新增失败");
            }
        }else {
            if(storeService.updateById(store)){
                return MessageUtil.createMessage(true,"修改成功");
            }else {
                return MessageUtil.createMessage(false,"修改失败");
            }
        }
    }

    @RequestMapping(value = "getStore", method = {RequestMethod.GET})
    @ResponseBody
    public String  getStore(){
        String ab = JSON.toJSONString(storeService.list());
        return  ab;
    }
}
